<!DOCTYPE html>












  


<html class="theme-next gemini use-motion" lang="zh-CN">
<head><meta name="generator" content="Hexo 3.8.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
































<link href="/blog/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css">

<link href="/blog/css/main.css?v=6.6.0" rel="stylesheet" type="text/css">


  <link rel="apple-touch-icon" sizes="180x180" href="/blog/images/apple-touch-icon-next.png?v=6.6.0">


  <link rel="icon" type="image/png" sizes="32x32" href="/blog/images/favicon-32x32-next.png?v=6.6.0">


  <link rel="icon" type="image/png" sizes="16x16" href="/blog/images/favicon-16x16-next.png?v=6.6.0">


  <link rel="mask-icon" href="/blog/images/logo.svg?v=6.6.0" color="#222">









<script id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/blog/',
    scheme: 'Gemini',
    version: '6.6.0',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":true,"onmobile":false},
    fancybox: false,
    fastclick: false,
    lazyload: false,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>


  




  <meta name="description" content="rm是remove的缩写，rm命令用于删除给定的文件和目录，可以说是 Linux 系统下最危险的命令。当想要删除当前目录下的所有文件时，如果不小心输入rm -rf . /*，后果自行体会一下。。。">
<meta name="keywords" content="Linux,Shell,每天一个Linux命令">
<meta property="og:type" content="article">
<meta property="og:title" content="每天一个Linux命令（8）：rm">
<meta property="og:url" content="http://seveninsight.github.io/blog/2019/01/07/每天一个Linux命令（8）：rm/index.html">
<meta property="og:site_name" content="七年弹指间的博客">
<meta property="og:description" content="rm是remove的缩写，rm命令用于删除给定的文件和目录，可以说是 Linux 系统下最危险的命令。当想要删除当前目录下的所有文件时，如果不小心输入rm -rf . /*，后果自行体会一下。。。">
<meta property="og:locale" content="zh-CN">
<meta property="og:updated_time" content="2019-01-11T03:24:30.176Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="每天一个Linux命令（8）：rm">
<meta name="twitter:description" content="rm是remove的缩写，rm命令用于删除给定的文件和目录，可以说是 Linux 系统下最危险的命令。当想要删除当前目录下的所有文件时，如果不小心输入rm -rf . /*，后果自行体会一下。。。">






  <link rel="canonical" href="http://seveninsight.github.io/blog/2019/01/07/每天一个Linux命令（8）：rm/">



<script id="page.configurations">
  CONFIG.page = {
    sidebar: "",
  };
</script>

  <title>每天一个Linux命令（8）：rm | 七年弹指间的博客</title>
  












  <noscript>
  <style>
    .use-motion .motion-element,
    .use-motion .brand,
    .use-motion .menu-item,
    .sidebar-inner,
    .use-motion .post-block,
    .use-motion .pagination,
    .use-motion .comments,
    .use-motion .post-header,
    .use-motion .post-body,
    .use-motion .collection-title { opacity: initial; }

    .use-motion .logo,
    .use-motion .site-title,
    .use-motion .site-subtitle {
      opacity: initial;
      top: initial;
    }

    .use-motion .logo-line-before i { left: initial; }
    .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope="" itemtype="http://schema.org/WebPage" lang="zh-CN">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope="" itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/blog/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">七年弹指间的博客</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
    
      
        <p class="site-subtitle">知是行之始，行是知之成</p>
      
    
  </div>

  <div class="site-nav-toggle">
    <button aria-label="切换导航栏">
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>



<nav class="site-nav">
  
    <ul id="menu" class="menu">
      
        
        
        
          
          <li class="menu-item menu-item-home">

    
    
    
      
    

    

    <a href="/blog/" rel="section"><i class="menu-item-icon fa fa-fw fa-home"></i> <br>首页</a>

  </li>
        
        
        
          
          <li class="menu-item menu-item-tags">

    
    
    
      
    

    

    <a href="/blog/tags/" rel="section"><i class="menu-item-icon fa fa-fw fa-tags"></i> <br>标签</a>

  </li>
        
        
        
          
          <li class="menu-item menu-item-categories">

    
    
    
      
    

    

    <a href="/blog/categories/" rel="section"><i class="menu-item-icon fa fa-fw fa-th"></i> <br>分类</a>

  </li>
        
        
        
          
          <li class="menu-item menu-item-archives">

    
    
    
      
    

    

    <a href="/blog/archives/" rel="section"><i class="menu-item-icon fa fa-fw fa-archive"></i> <br>归档</a>

  </li>

      
      
    </ul>
  

  

  
</nav>



  



</div>
    </header>

    


    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          
            

          
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  

  <article class="post post-type-normal" itemscope="" itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://seveninsight.github.io/blog/blog/2019/01/07/每天一个Linux命令（8）：rm/">

    <span hidden itemprop="author" itemscope="" itemtype="http://schema.org/Person">
      <meta itemprop="name" content="七年弹指间">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/blog/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope="" itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="七年弹指间的博客">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">每天一个Linux命令（8）：rm

              
            
          </h1>
        

        <div class="post-meta">
          <span class="post-time">

            
            
            

            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              

              
                
              

              <time title="创建时间：2019-01-07 11:19:22" itemprop="dateCreated datePublished" datetime="2019-01-07T11:19:22+08:00">2019-01-07</time>
            

            
              

              
                
                <span class="post-meta-divider">|</span>
                

                <span class="post-meta-item-icon">
                  <i class="fa fa-calendar-check-o"></i>
                </span>
                
                  <span class="post-meta-item-text">更新于</span>
                
                <time title="修改时间：2019-01-11 11:24:30" itemprop="dateModified" datetime="2019-01-11T11:24:30+08:00">2019-01-11</time>
              
            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope="" itemtype="http://schema.org/Thing"><a href="/blog/categories/Linux/" itemprop="url" rel="index"><span itemprop="name">Linux</span></a></span>

                
                
                  ，
                
              
                <span itemprop="about" itemscope="" itemtype="http://schema.org/Thing"><a href="/blog/categories/Linux/每天一个Linux命令/" itemprop="url" rel="index"><span itemprop="name">每天一个Linux命令</span></a></span>

                
                
              
            </span>
          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <p><code>rm</code>是<code>remove</code>的缩写，<code>rm</code>命令用于删除给定的文件和目录，可以说是 Linux 系统下最危险的命令。<br>当想要删除当前目录下的所有文件时，如果不小心输入<code>rm -rf . /*</code>，后果自行体会一下。。。</p>
<a id="more"></a>
<h2 id="1-功能"><a href="#1-功能" class="headerlink" title="1 功能"></a>1 功能</h2><p><code>rm</code>命令用于删除某个目录下的一个或多个文件或目录。<br>如果没有使用<code>-r</code>选项，则 rm 不会删除目录。对于链接文件，只是删除这个链接文件，原有文件保持不变。</p>
<h2 id="2-命令格式"><a href="#2-命令格式" class="headerlink" title="2 命令格式"></a>2 命令格式</h2><pre><code>rm [选项] [文件或者目录]
</code></pre><p>【选项说明】</p>
<pre><code>-i：删除文件或目录之前先提示用户
-f：忽略不存在的文件，从不提示
-r或-R：递归删除，将参数中列出的目录全部删除，包括子目录和子文件
-v：显示指令的详细执行过程
--help：显示此帮助信息并退出
--version：输出版本信息并退出
</code></pre><p>【参数说明】</p>
<ul>
<li>参数可以是一个或者多个文件或者目录</li>
<li>当参数是目录时，需要<code>-r</code>选项才能删除目录</li>
</ul>
<h2 id="3-常见用法与示例"><a href="#3-常见用法与示例" class="headerlink" title="3 常见用法与示例"></a>3 常见用法与示例</h2><h3 id="3-1-删除文件"><a href="#3-1-删除文件" class="headerlink" title="3.1 删除文件"></a>3.1 删除文件</h3><pre><code>rm -f 文件名 文件名 ...
</code></pre><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 删除文件</span></span><br><span class="line">[work@testpc old 10:28:31]rm aaa.txt bbb.txt</span><br><span class="line"></span><br><span class="line"><span class="comment"># 再次删除，提示文件不存在</span></span><br><span class="line">[work@testpc old 10:28:36]$ rm aaa.txt</span><br><span class="line">rm: cannot remove `aaa.txt<span class="string">': No such file or directory</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string"># 加 -f 选项，文件不存在时不提示</span></span><br><span class="line"><span class="string">[work@testpc old 10:28:40]$ rm -f aaa.txt</span></span><br></pre></td></tr></table></figure>
<h3 id="3-2-删除目录"><a href="#3-2-删除目录" class="headerlink" title="3.2 删除目录"></a>3.2 删除目录</h3><pre><code>rm -rf 目录名 目录名 ...
</code></pre><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 删除目录</span></span><br><span class="line">[work@testpc old 10:38:21]$ rm -r dir0 dir1</span><br><span class="line"></span><br><span class="line"><span class="comment"># 再次删除，提示文件不存在</span></span><br><span class="line">[work@testpc old 10:38:28]$ rm -r dir0 dir1</span><br><span class="line">rm: cannot remove `dir0<span class="string">': No such file or directory</span></span><br><span class="line"><span class="string">rm: cannot remove `dir1'</span>: No such file or directory</span><br><span class="line"></span><br><span class="line"><span class="comment"># 加 -f 选项，文件不存在时不提示</span></span><br><span class="line">[work@testpc old 10:38:44]$ rm -rf dir0 dir1</span><br></pre></td></tr></table></figure>
<h3 id="3-3-删除以-开头的文件"><a href="#3-3-删除以-开头的文件" class="headerlink" title="3.3 删除以-开头的文件"></a>3.3 删除以<code>-</code>开头的文件</h3><p>删除以<code>-</code>开头的文件时，有两种方法：</p>
<p><strong>1、在文件名前加<code>--</code></strong></p>
<pre><code>rm -- -foo
</code></pre><p><code>--</code>选项后面必须是文件名，如果要加其他选项，其他选项必须在<code>--</code>之前。如果不加<code>--</code>选项，会把以<code>-</code>开头的文件误认为是命令选项。</p>
<p><strong>2、在文件名前加上路径</strong></p>
<pre><code>rm ./-foo
</code></pre><p>例如：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">[work@testpc old 10:52:09]$ touch -- -aaa</span><br><span class="line"></span><br><span class="line"><span class="comment"># 不加 --，误认是命令的功能选项</span></span><br><span class="line">[work@testpc old 10:52:48]$ rm -f -aaa</span><br><span class="line">rm: invalid option -- <span class="string">'a'</span></span><br><span class="line">Try `rm ./-aaa<span class="string">' to remove the file `-aaa'</span>.</span><br><span class="line">Try `rm --<span class="built_in">help</span><span class="string">' for more information.</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string"># 文件名前加 --</span></span><br><span class="line"><span class="string">[work@testpc old 10:53:24]$ rm -- -aaa</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">[work@testpc old 10:53:36]$ rm -- -aaa</span></span><br><span class="line"><span class="string">rm: cannot remove `-aaa'</span>: No such file or directory</span><br><span class="line"></span><br><span class="line"><span class="comment"># 删除多个以 - 开头的文件</span></span><br><span class="line">[work@testpc old 10:54:02]$ rm -f -- -aaa -bbb</span><br></pre></td></tr></table></figure>

      
    </div>

    

    
    
    

    

    
      
    
    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/blog/tags/Linux/" rel="tag"># Linux</a>
          
            <a href="/blog/tags/Shell/" rel="tag"># Shell</a>
          
            <a href="/blog/tags/每天一个Linux命令/" rel="tag"># 每天一个Linux命令</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/blog/2019/01/07/每天一个Linux命令（7）：mv/" rel="next" title="每天一个Linux命令（7）：mv">
                <i class="fa fa-chevron-left"></i> 每天一个Linux命令（7）：mv
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>


  </div>


          </div>
          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope="" itemtype="http://schema.org/Person">
            
              <p class="site-author-name" itemprop="name">七年弹指间</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          
            <nav class="site-state motion-element">
              
                <div class="site-state-item site-state-posts">
                
                  <a href="/blog/archives/">
                
                    <span class="site-state-item-count">12</span>
                    <span class="site-state-item-name">日志</span>
                  </a>
                </div>
              

              
                
                
                <div class="site-state-item site-state-categories">
                  <a href="/blog/categories/index.html">
                    
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                    <span class="site-state-item-count">7</span>
                    <span class="site-state-item-name">分类</span>
                  </a>
                </div>
              

              
                
                
                <div class="site-state-item site-state-tags">
                  <a href="/blog/tags/index.html">
                    
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                    <span class="site-state-item-count">10</span>
                    <span class="site-state-item-name">标签</span>
                  </a>
                </div>
              
            </nav>
          

          

          

          

          
          

          
            
          
          

        </div>
      </div>

      
      <!--noindex-->
        <div class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-功能"><span class="nav-text">1 功能</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-命令格式"><span class="nav-text">2 命令格式</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-常见用法与示例"><span class="nav-text">3 常见用法与示例</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#3-1-删除文件"><span class="nav-text">3.1 删除文件</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-2-删除目录"><span class="nav-text">3.2 删除目录</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-3-删除以-开头的文件"><span class="nav-text">3.3 删除以-开头的文件</span></a></li></ol></li></ol></div>
            

          </div>
        </div>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2019</span>
  <span class="with-love" id="animate">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">七年弹指间</span>

  

  
</div>


  <div class="powered-by">由 <a href="https://hexo.io" class="theme-link" rel="noopener" target="_blank">Hexo</a> 强力驱动 v3.8.0</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 – <a href="https://theme-next.org" class="theme-link" rel="noopener" target="_blank">NexT.Gemini</a> v6.6.0</div>




        








        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
          <span id="scrollpercent"><span>0</span>%</span>
        
      </div>
    

    

    

    
  </div>

  

<script>
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>


























  
  
    <script src="/blog/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script src="/blog/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script src="/blog/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  


  


  <script src="/blog/js/src/utils.js?v=6.6.0"></script>

  <script src="/blog/js/src/motion.js?v=6.6.0"></script>



  
  


  <script src="/blog/js/src/affix.js?v=6.6.0"></script>

  <script src="/blog/js/src/schemes/pisces.js?v=6.6.0"></script>



  
  <script src="/blog/js/src/scrollspy.js?v=6.6.0"></script>
<script src="/blog/js/src/post-details.js?v=6.6.0"></script>



  


  <script src="/blog/js/src/bootstrap.js?v=6.6.0"></script>



  



  











  





  

  

  

  

  
  

  
  

  


  
  

  

  

  

  

  

  

</body>
</html>
